package com.hhit.collaborative_learning.community;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.BBS_ReplyDao;
import com.hhit.collaborative_learning.dao.BBS_ReplyMessageDao;
import com.hhit.collaborative_learning.dao.impl.BBS_ReplyDaoImpl;
import com.hhit.collaborative_learning.dao.impl.BBS_ReplyMessageDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.ResponseUtil;

/**
 * Servlet implementation class PutReplyServlet
 */
@WebServlet("/community/PutReplyServlet")
public class PutReplyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PutReplyServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String textArea = request.getParameter("textArea");
		String id = request.getParameter("id");
		String user_id = request.getParameter("user_id");
		String tyle = request.getParameter("tyle");
		System.out.println("textArea  "+textArea);
		Connection con = DBAccess.getInstance().getConn();
		JSONObject jobj = new JSONObject();
		switch (tyle) {
		case "0":
			System.out.println("类型  "+tyle+" 盖楼");
			jobj = insertReply(con, textArea, id, user_id);
			break;
		case "1":
			System.out.println("类型  "+tyle+" 回复楼层");
			jobj = insertReplyM(con, textArea, id, user_id);
			break;
		default:
			System.out.println("未知类型  "+tyle);
			break;
		}

		try {
			ResponseUtil.write(response, jobj.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(con, null, null);
		}
	}

	private JSONObject insertReply(Connection con, String text, String tid,
			String uid) {
		BBS_ReplyDao rd = new BBS_ReplyDaoImpl();
		JSONObject jobj = new JSONObject();
		String isSuccess = "1";
		int re = rd.insert(con, text, tid, uid);
		if (re != 0) {
			isSuccess = "0";
		}
		jobj.put("isSuccess", isSuccess);
		return jobj;
	}

	private JSONObject insertReplyM(Connection con, String text, String rid,
			String uid) {
		BBS_ReplyMessageDao rmd = new BBS_ReplyMessageDaoImpl();
		JSONObject jobj = new JSONObject();
		String isSuccess = "1";
		int re = rmd.insert(con, text, rid, uid);
		if (re != 0) {
			isSuccess = "0";
		}
		jobj.put("isSuccess", isSuccess);
		return jobj;

	}
}
